<?php
/**
 * Filter 过滤函数库
 * 支持常规请求和ajax请求
 * 
 * @author hary
 * @version 1.0
 */



/**
 * 登录检测
 * 
 * @param $request		请求参数，这个参数在filter中是必需的
 * @return 
 */
function checkLogin($request)
{
	
	if(isAjaxRequest())
	{
		if (empty($_SESSION['pay']['login_info']['user_id']))
		{
			$isSeccode = $_SESSION['login_error_num'] > 2;
			echo json_encode(array('uid'=>0,'isSeccode'=>$isSeccode));
			exit;
		}
		else if($request['withLogin'])
		{
			echo json_encode(array('uid'=>$_SESSION['pay']['login_info']['user_id']));
			exit;
		}
	}
	else
	{
		if (empty($_SESSION['pay']['login_info']['user_id']))
		{
			$_SESSION['pay']['url_pre'] = $_SERVER['REQUEST_URI'];
			#Goto(get_url('user','index'),'header',false);
			Goto(get_url('main','illegalLogin'),'header','document',false);
			exit;
		}
	}
}

/**
 * 是否已经登陆
 */
function isLogin()
{
	if( !isset($_SESSION['SURVEY']['USER']))
	{
		if(isAjaxRequest())
		{
			echo "<script>location.href='/admin/admin_common/login';</script>";
			exit;
		}
		else
		{
			Goto('http://www.61.com','header','document',false);
		}
	}
}

/**
 * 是否已经登陆
 */
function mainTain()
{
	Goto("/html/page/maintain/index.html",'header','document',false);
}

/**
 * 禁止通行
 * @return unknown_type
 */
function forbid()
{
	Goto(get_url('admin/admin_common','forbid'),'header','document',false);
}


/**
 * 参数输入校验方法
 * @return unknown_type
 */
function showParams($request)
{
	print_r($request);
}

/**
 * 验证码日志
 * @return unknown_type
 */
function logYzm()
{
	if($_POST)
	{
		$seccode = trim(strtolower($_POST['seccode']));
		$sec     = trim(strtolower($_SESSION['login_check_number']));
		$userid  = $_POST['userid'];
		
		$error  = "验证码检测输出：";
		$error .= "HOST： {$_SERVER['HTTP_HOST']}";
		$error .= "IP： {$_SERVER['REMOTE_ADDR']}";
		$error .= "米米号： {$userid}";
		$error .= "输入验证码：{$seccode} 系统验证码：{$sec}";
		
		# 返回失败
		Log::write($error);
	}
}

/**
 * 
 * 判断是否是ajax请求
 * @return 
 */
function isAjaxRequest()
{
	if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
	{
		return true;
	}
	else 
	{
		return false;	
	}
}


